package essay

import (
	"log"
	"net/http"

	essayStore "gitee.com/taadis/nook/store/essay"
)

func init() {
	log.Println("init /essay/remove/ essayRemove")
	http.HandleFunc("/essay/remove/", essayRemove)
}

func essayRemove(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodPost:
		postEssayRemove(w, r)
	default:
		http.Error(w, "", http.StatusMethodNotAllowed)
	}
}

func postEssayRemove(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	id := r.FormValue("id")
	if id == "" {
		message := "随笔编号是必须的"
		log.Println(message)
		http.Error(w, message, http.StatusBadRequest)
		return
	}
	err := essayStore.Remove(id)
	if err != nil {
		log.Println(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	http.Redirect(w, r, "/", http.StatusFound)
}
